package com.java47.controller;

import com.java47.entity.User;
import com.java47.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @authoru java1214
 * @create 2023-01-04 14:04
 */
@RestController
@CrossOrigin //允许跨域,其他服务器可以向该地址发情求
public class UserController {
    @Autowired
    private Userservice userservice;

    //查询
    @GetMapping(value = "/user/findAll")
    public List<User> findAll() {
        List<User> userList = userservice.findAll();
        return userList;
    }

    //根据id查询
    @GetMapping(value = "/user/findById/{id}")
    public User findById(@PathVariable("id") Long id) {
        User user = userservice.findById(id);
        return user;
    }

    //添加
    @PostMapping(value = "/user/add")
    public Map<String, Object> add(User user) {
        Map<String, Object> map = userservice.add(user);
        return map;
    }

    //更新
    @PostMapping(value = "/user/update")
    public Map<String, Object> update(User user) {
        Map<String, Object> map = userservice.update(user);
        return map;
    }

    //删除
    @GetMapping(value = "/user/deleteById/{id}")
    public Map<String, Object> deleteById(@PathVariable("id") Long id) {
        Map<String, Object> map = userservice.deleteById(id);
        return map;
    }
}
